import json
import subprocess

from csv_tool import Csv
from excel_tool import Excel
from mysql_tool import MySql
import requests


def get_data_from_csv(file_path: str, from_cell: int, row_nos: list):
    """
    从 CSV 获取数据
    :param file_path:
    :param from_cell:
    :param row_nos:
    :return:
    """

    def get_data_fun(row: list):
        rows = []
        for row_no in row_nos:
            rows.append(row[row_no].strip())
        return rows

    return Csv.iter_csv(get_data_fun, None, from_cell, file_path)


def get_data_from_excel(file_path: str, from_cell: int, row_nos: list):
    """
    从 Excel 获取数据
    :param file_path:
    :param from_cell:
    :param row_nos:
    :return:
    """

    def get_data_fun(row: list):
        rows = []
        for row_no in row_nos:
            rows.append(row[row_no].strip())
        return rows

    return Excel.iter_excel_calamine(get_data_fun, None, from_cell, file_path)


def get_data_from_mysql():
    """
    从数据库获取数据
    :return:
    """
    # 动态生成多个占位符
    # uuids = []
    # in_placeholder = ','.join(['%s'] * len(uuids))
    # sql = f'select id, uuid from show_order_detail where uuid in ({in_placeholder})'
    # database = dict(host='devserver6', user='theatre', password='Theatre!GLSM2022', db='theatre', port=3306)
    # result = MySql.get_data(database, sql, uuids)
    sql = f"select id, mzx_member_id, point_value, trade_no, date_format(spend_time, '%Y-%m-%d+%h:%i:%s') as spend_time, 'spend' as spend_type, real_exserial, 2088 as company_id, remark from point_discount_2088_repair where id >= 284959"
    database = dict(host='devserver6', user='thvendor', password='ThVendor!1234', db='thvendor', port=3306)
    result = MySql.get_data(database, sql)
    return result


def insert_mysql(values: list):
    """
    批量数据导入
    :param values:
    :return:
    """
    sql = 'INSERT INTO coupon_batch_refund_trade_no (trade_no) VALUES (%s)'
    database = dict(host='devserver6', user='mall', password='mall123', db='mall', port=3306)
    MySql.insert(database, sql, values)


def curl_fun(values: list):
    """
    审核退单
    :param values:
    :return:
    """
    command = "curl 'https://admin.antank.cn/thvendor/admin/temp/pointSpend.xhtml?memberId={}&spendPoint={}&tradeNo={}&spendTime={}&spendType={}&exserial={}&companyId={}&remark={}' \
          -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
          -H 'accept-language: zh-CN,zh;q=0.9' \
          -H 'cookie: fctkid=1704688446615_FQu6_92a5@2BF55B47A7AD9E00; Authorization=userbiMcX5XRFtQKBbwGGeXjc2f20f32; ukeyage=1739428363945' \
          -H 'dnt: 1' \
          -H 'priority: u=0, i' \
          -H 'sec-ch-ua: \"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"' \
          -H 'sec-ch-ua-mobile: ?0' \
          -H 'sec-ch-ua-platform: \"macOS\"' \
          -H 'sec-fetch-dest: document' \
          -H 'sec-fetch-mode: navigate' \
          -H 'sec-fetch-site: none' \
          -H 'sec-fetch-user: ?1' \
          -H 'upgrade-insecure-requests: 1' \
          -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'"
    for row in values:
        cmd = command.format(row['mzx_member_id'], row['point_value'], row['trade_no'], row['spend_time'], row['spend_type'], row['real_exserial'], row['company_id'], requests.utils.quote(row['remark']))
        print(row['id'])
        print(row['real_exserial'])
        # print(cmd)
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
        print(f"Output: {result.stdout}")
        # print(f"Error: {result.stderr}")


if __name__ == '__main__':
    # values = get_data_from_excel('/Users/jl/Documents/refund/副本学校名称.xlsx', 0, [0, 1])
    # print(values)
    # curl_fun(values)
    result = get_data_from_mysql()
    curl_fun(result)
